package com.ttsx.controller;

import com.ttsx.core.common.util.WebUtils;
import com.ttsx.pojo.dto.order.OrderQueryDTO;
import com.ttsx.pojo.dto.product.ProductQueryDTO;
import com.ttsx.pojo.entity.User;
import com.ttsx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 页面控制器
 *
 * @author wb
 * @since 2025-03-01
 */

@Controller
public class PageController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;
    @Autowired
    private CartService cartService;
    @Autowired
    private UserAddressService userAddressService;
    @Autowired
    private OrderService orderService;

    @GetMapping(value = {"/", "/index"})
    public String index(Model model, HttpServletRequest request) {
        model.addAttribute("categories", categoryService.getCategoryList());
        return "index";
    }

    @GetMapping("/list")
    public String list(Model model,
                       @RequestParam(defaultValue = "1") Integer pIndex,
                       @RequestParam(defaultValue = "10") Integer pSize,
                       ProductQueryDTO productQueryDTO) {

        model.addAttribute("categories", categoryService.getCategoryList());
        model.addAttribute("product", productQueryDTO);
        model.addAttribute("productPage", productService.getPage(pIndex, pSize, productQueryDTO));

        return "list";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @GetMapping("/profile")
    public String profile(HttpServletRequest request) {
        if (checkLogin(request)) {
            return "redirect:login";
        }
        return "user-info";
    }

    @GetMapping("/cart")
    public String cart(Model model, HttpServletRequest request) {
        if (checkLogin(request)) {
            return "redirect:login";
        }
        model.addAttribute("carts", cartService.getByUserId(request));

        return "cart";
    }

    @GetMapping("/cart/delete/{id}")
    public String cartDelete(@PathVariable("id") Long id) {
        cartService.removeById(id);
        return "redirect:/cart";
    }

    @GetMapping("/place-order")
    public String payOrder(HttpServletRequest request) {
        if (checkLogin(request)) {
            return "redirect:login";
        }
        return "place-order";
    }

    @GetMapping("/addr")
    public String addr(Model model, HttpServletRequest request) {
        if (checkLogin(request)) {
            return "redirect:login";
        }

        model.addAttribute("address", userAddressService.getByUserId());

        return "user-addr";
    }

    @GetMapping("/addr/delete/{id}")
    public String addrDelete(@PathVariable("id") Long id) {
        userAddressService.removeById(id);
        return "redirect:/addr";
    }


    @GetMapping("/order")
    public String order(Model model, @RequestParam(defaultValue = "1") Integer pIndex, @RequestParam(defaultValue = "3") Integer pSize) {
        HttpServletRequest request = WebUtils.getRequest();

        if (checkLogin(request)) {
            return "redirect:login";
        }

        User user = (User) request.getSession().getAttribute("user");
        OrderQueryDTO orderQueryDTO = new OrderQueryDTO();
        orderQueryDTO.setUserId(user.getId());

        model.addAttribute("page", orderService.getPage(pIndex, pSize, orderQueryDTO));

        return "user-order";
    }

    @GetMapping("/order/delete/{id}")
    public String orderDelete(@PathVariable("id") Long id) {
        orderService.removeById(id);
        return "redirect:/order";
    }

    @GetMapping("/order/cancel/{id}")
    public String orderCancel(@PathVariable("id") Long id) {
        orderService.orderCancel(id);
        return "redirect:/order";
    }

    @GetMapping("/product/{id}")
    public String product(Model model, @PathVariable("id") Long id) {
        model.addAttribute("product", productService.getById(id));
        return "detail";
    }


    @GetMapping("/buy/{ids}")
    public String buy(@PathVariable("ids") List<Long> cartIds, Model model, HttpServletRequest request) {
        if (checkLogin(request)) {
            return "redirect:/login";
        }

        model.addAttribute("address", userAddressService.getByUserId());
        model.addAttribute("carts", cartService.getByCartIds(cartIds));

        return "place-order";
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        // 清除session
        request.getSession().invalidate();
        return "login";
    }

    private Boolean checkLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        return user == null;
    }
}
